package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.service.db.PmsProductAttributeService;
import com.itheima.service.db.PmsProductCategoryAttributeRelationService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.PmsAttrInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@Component
public class PmsProductAttributeManager {
    @DubboReference
    PmsProductAttributeService productAttributeService;
    @DubboReference
    PmsProductCategoryAttributeRelationService pmsProductCategoryAttributeRelationService;


    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    public R findAttributeByPage(Integer cid, Integer pageNum, Integer pageSize, String type) {
        PageBeanVo pageBeanVo = productAttributeService.findAttributeByPage(cid, pageNum, pageSize, type);
        return R.success(pageBeanVo);
    }

    /**
     * 根据商品分类的id获取商品属性及属性分类
     * @param productCategoryId
     * @return
     */
    public R findAttributeById(Long productCategoryId) {
        List<PmsAttrInfoVo> voList = new ArrayList<>();
        //1.查询中间表通过分类di获得属性id
        List<Long> attributeIds = pmsProductCategoryAttributeRelationService.findAttributeId(productCategoryId);
        //2.遍历属性id查询属性分类id;一条属性只属于一个分类
        if (CollUtil.isNotEmpty(attributeIds)) {
            for (Long attributeId : attributeIds) {
                //3.根据属性id查询属性分类id
                PmsProductAttribute productAttribute = productAttributeService.findById(attributeId);
                Long attributeCategoryId = productAttribute.getProductAttributeCategoryId();
                //4.封装数据
                PmsAttrInfoVo vo = new PmsAttrInfoVo();
                vo.setAttributeId(attributeId);
                vo.setAttributeCategoryId(attributeCategoryId);
                voList.add(vo);
            }
        }
        //5.返回数据
        return R.success(voList);
    }


    /**
     * 添加商品属性信息
     * @param productAttributeParam
     * @return
     */
    public R addAttribute(PmsProductAttribute productAttributeParam) {
        Boolean res = productAttributeService.addAttribute(productAttributeParam);
        return res? R.success("添加属性成功"):R.error(500,"属性名称冲突,添加失败");
    }

    /**
     * 批量删除属性
     * @param ids
     * @return
     */
    public R deleteByIds(List<Long> ids) {
        productAttributeService.deleteByIds(ids);
        return R.success("批量删除成功");
    }

    /**
     * 修改商品属性信息
     * @param id
     * @param productAttributeParam
     * @return
     */
    public R updateById(Long id, PmsProductAttribute productAttributeParam) {
        Boolean res = productAttributeService.updateById(id,productAttributeParam);
        return res? R.success("修改属性成功"):R.error(500,"属性名称冲突,修改失败");
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    public R findById(Long id) {
        PmsProductAttribute productAttribute = productAttributeService.findById(id);
        return R.success(productAttribute);
    }
}
